<?php

include 'DBconn.php';

class WarehouseMaster {

    public function getBranch() {
        $res = mysql_query("select serialnumber,branch_name from branch_header where activestatus=1 ") or die(mysql_error());
        $ret = array();
        while ($row = mysql_fetch_object($res)) {
            $ret[] = $row;
        }
        return $ret;
    }

    public function getAllWarehouses() {
        $result = mysql_query("select * from warehouse_header where activestatus=1") or die(mysql_error());
        $ret_arr = array();
        while ($row = mysql_fetch_object($result)) {
            $tmp = new VOWarehouseMaster();
            $tmp->serialnumber = (int) $row->serialnumber;
            $tmp->warehouse_id = $row->warehouse_id;
            $tmp->warehouse_name = $row->warehouse_name;
            $tmp->warehouse_description = $row->warehouse_description;
            $tmp->warehouse_email = $row->warehouse_email;
            $tmp->branch_serialnumber = (int) $row->branch_serialnumber;
            $tmp->cre_userid = (int) $row->cre_userid;
            $tmp->cre_datetime = $row->cre_datetime;
            $tmp->activestatus = (boolean) $row->activestatus;
            $ret_arr[] = $tmp;
        }
        mysql_free_result($result);
        return $ret_arr;
    }

    public function addNewWarehouse($VObject) {
        $query = "insert into warehouse_header(warehouse_id,warehouse_name,warehouse_description,warehouse_email,branch_serialnumber,cre_userid,cre_datetime,activestatus) values('$VObject->warehouse_id','$VObject->warehouse_name','$VObject->warehouse_description','$VObject->warehouse_email','$VObject->branch_serialnumber','$VObject->cre_userid','$VObject->cre_datetime',1)";
        $s = mysql_query($query) or die(mysql_error());
        $last_id = mysql_insert_id();

        $result = mysql_query("SELECT serialnumber from product_header where activestatus=1") or die(mysql_error());
        $ret = array();
        while ($row = mysql_fetch_object($result)) {
            $query1 = "insert into stock_header(warehouse_serialnumber_fk,pro_serialnumber,on_hand,on_sale,in_order,sold,cre_userid,cre_datetime,activestatus) values('$last_id','$row->serialnumber',0,0,0,0,'$VObject->cre_userid','$VObject->cre_datetime',1)";
            mysql_query($query1) or die(mysql_error());
        }

        return $this->getAllWarehouses();
    }

    public function deleteWarehouse($serialnumber) {
        $s = mysql_query("update warehouse_header set activestatus=0 where serialnumber='$serialnumber'") or die(mysql_error());
        $s1 = mysql_query("update stock_header set activestatus=0 where warehouse_serialnumber_fk='$serialnumber'") or die(mysql_error());
        if ($s = 1) {
            mysql_query("COMMIT");
        } else {
            mysql_query("ROLLBACK");
        }
        return $serialnumber;
    }

    public function editWarehouse($VObject) {
        $query = "update warehouse_header set warehouse_id='$VObject->warehouse_id',warehouse_name='$VObject->warehouse_name',warehouse_description='$VObject->warehouse_description',warehouse_email='$VObject->warehouse_email',branch_serialnumber='$VObject->branch_serialnumber' where serialnumber='$VObject->serialnumber'";
        $s = mysql_query($query) or die(mysql_error());
        if ($s = 1) {
            mysql_query("COMMIT");
        } else {
            mysql_query("ROLLBACK");
        }
        return $this->getAllWarehouses();
    }

}

class VOWarehouseMaster {

    var $serialnumber;
    var $warehouse_id;
    var $warehouse_name;
    var $warehouse_description;
    var $warehouse_email;
    var $branch_serialnumber;
    var $cre_userid;
    var $cre_datetime;
    var $activestatus;

}

?>